package com.ruifu.doc.controller;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.doc.service.DmFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * @author 宁义锐
 */
@RestController
@CrossOrigin
@RequestMapping("/dmFile")
public class DmFileController {

    @Autowired
    private DmFileService dmFileService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser= SecurityUtils.getUser();
    }

    /**
     * 文件上传
     *
     * @param file 文件对象
     * @return 文件访问url
     */
    @PostMapping(value = "/uploadFile")
    public ResultVO uploadFile(MultipartFile file) {
        try {
            Long fiFileId = dmFileService.uploadFile(file, cloudUser);
            return new ResultVO(true, fiFileId + "");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 文件下载
     *
     * @param fiFileId 文件id
     * @param response
     */
    @GetMapping(value = "/downloadFile")
    public void downloadFile(Long fiFileId, HttpServletResponse response) {
        try {
            dmFileService.downloadFile(fiFileId, response, cloudUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量文件下载
     *
     * @param fiFileIds 文件id数组
     * @param response
     */
    @PostMapping(value = "/downloadPLFile")
    public void downloadPLFile(Long[] fiFileIds, HttpServletResponse response) {
        try {
            dmFileService.downloadPLFile(fiFileIds, response, cloudUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件删除
     *
     * @param fiFileId 文件Id
     * @return
     */
    @GetMapping(value = "/deleteFile/{fiFileId}")
    public ResultVO deleteFile(@PathVariable Long fiFileId) {
        try {
            dmFileService.deleteFile(fiFileId, cloudUser);
            return new ResultVO(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 更换文件
     *
     * @param fiFileId 原文件信息ID
     * @param file     新文件
     * @return
     */
    @PostMapping(value = "/changeFile/{fiFileId}")
    public ResultVO changeFile(@PathVariable("fiFileId") Long fiFileId, @RequestBody MultipartFile file) {
        try {
            dmFileService.changeFile(fiFileId, file, cloudUser);
            return new ResultVO(true, "更换文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 复制文件
     *
     * @param fiFileId 文件id
     * @return 复制后的文件id
     */
    @GetMapping(value = "/copyFile")
    public ResultVO copyFile(Long fiFileId) {
        try {
            Long file = dmFileService.copyFile(fiFileId, cloudUser);
            return new ResultVO(true, file + "");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 预览文件
     *
     * @param fiFileId 文件id
     * @return 文件访问路径
     */
    @GetMapping(value = "/previewFile/{fiFileId}")
    public ResultVO previewFile(@PathVariable Long fiFileId) {
        try {
            String url = dmFileService.previewFile(fiFileId, cloudUser);
            return new ResultVO(true, url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }
}
